﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ASPItalia.ModelVirtualCasting.Common;
using ASPItalia.ModelVirtualCasting.Web.Utils;
using ASPItalia.ModelVirtualCasting.ObjectModel;
using ASPItalia.ModelVirtualCasting.Web.Models;

namespace ASPItalia.ModelVirtualCasting.Web.Controllers
{
	public class PersonController : Controller
	{
		private IPersonRepository personRepository;

		internal PersonController(IPersonRepository personRepository)
		{
			this.personRepository = personRepository;
		}

		public PersonController()
			: this(ApplicationContext.Current.People)
		{ }

		public ActionResult Profile(string nickname, bool? disableAjax)
		{
			var person = this.getPersonByNick(nickname);

			if (person == null)
				this.TempData["Message"] = "Hai per caso provato a taroccare la URL?";

			var selectedPerson = person??new Candidate();
			this.ViewData.Model = new ProfileModel()
				{
					Person = selectedPerson,
					EditControllerName = selectedPerson.IsModel ? "Model" : "Candidate"
				};

			#region AJAX
			if (Request.IsAjaxRequest() && !disableAjax.GetValueOrDefault())
				return this.View("ShowPerson", person);
			#endregion

			return this.View();
		}

		private Person getPersonByNick(string nickname)
		{
			IPersonRepository repository = this.personRepository;

			Person person = repository
				.Include("Multimedia").Include("NationalityCountry").Include("ResidenceCountry")
				.Where(c => c.NickName.ToLower() == nickname.ToLower()).SingleOrDefault();

			return person;
		}

		public ActionResult ShowMultimedia(string nickname)
		{
			IPersonRepository repository = this.personRepository;

			Person person = repository
				.Include("Multimedia")
				.Where(c => c.NickName.ToLower() == nickname.ToLower()).SingleOrDefault();

			this.ViewData.Model = person;

			return this.View();
		}

		public ActionResult ShowMultimediaDetails(string nickname, int id)
		{
			IPersonRepository repository = this.personRepository;

			var results = repository
				.Include("Multimedia")
				.Where(c => c.NickName.ToLower() == nickname.ToLower()).SelectMany(x => x.Multimedia).Where(x => x.MultimediaID == id).SingleOrDefault();

			this.ViewData.Model = new MultimediaDTO { Nickname = nickname, Multimedia = results };

			return this.View();
		}

        public ActionResult Gallery(int id, int index = 0)
        {
            var person = this.personRepository
                .Include("Multimedia")
                .Where(p => p.ID == id)
                .Single();

            var model = new GalleryViewModel();
            model.Id = id;
            model.Nickname = person.NickName;

            var images = person.Multimedia.OfType<Image>().ToList();

            model.PreviousIndex = index == 0 ? images.Count - 1 : index - 1;
            model.NextIndex = index == images.Count - 1 ? 0 : index + 1;

            model.ImageUrl = images[index].FileName.ToAbsoluteUrl();
            model.Index = index;

            return this.View(model);
        }
	}
}